HTML2PDF.js Tutorial to Convert HTML to PDF Document With Page Break in Browser Using Javascript

您所在的位置:网站首页 html2pdf react HTML2PDF.js Tutorial to Convert HTML to PDF Document With Page Break in Browser Using Javascript

HTML2PDF.js Tutorial to Convert HTML to PDF Document With Page Break in Browser Using Javascript

2023-02-23 15:15| 来源: 网络整理| 查看: 265

Welcome folks today in this blog post we will be exporting the html document to pdf document with page break in browser using javascript. All the full source code of the application is shown below.

Get Started

In order to get started you need to make a index.html file and copy paste the following code

First of all we will be including the cdn of html2pdf library as shown below

12345678910聽聽聽聽html2pdf.js page break聽聽聽聽

And as you can see in the above code we are including the cdn of html2pdf.js library as shown above. And now we need to write some html that we need to export to pdf document as shown below

12345聽聽聽聽聽聽聽聽First Content聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽second content聽聽聽聽

As you can see we have the div parent element and we have given id to it so that we can target it using the javascript. And then we have a div with some content and then we have a page break class which is html2pdf.js page break class which is called html2pdf__page-break you need to write the same class because this class will make sure it will add the page break after this the content will be shifted to the second page in the pdf document. And after this we are defining one other div element with some content. And now we simply need to export to pdf document by adding some javascript code as shown below

Now first of all we will be targeting the parent div element which has got the id attached to it as shown below

JavaScript1let element = document.getElementById('element')

Now we simply need to call the html2pdf() method and pass this element to it as shown below

JavaScript1let element = document.getElementById('element')

Now if you open the application the pdf file will be downloaded automatically as an attachment as you can see

As you can see the content is split up into 2 pages with page break but there is some problem out there. We need to add some margin and padding to the text. So we can provide some config options to the html2pdf() constructor as shown below

JavaScript1234567html2pdf(element,{聽聽聽聽margin:10,聽聽聽聽filename:'output.pdf',聽聽聽聽image:{type:'jpeg',quality:0.98},聽聽聽聽html2canvas:{scale:2,logging:true,dpi:192,letterRendering:true},聽聽聽聽jsPDF:{unit:'mm',format:'a4',orientation:'portrait'}})

As you can see we are providing some custom options such as the margin which is in numbers and then we are also passing the custom filename to the pdf document and also adding the image quality and type parameter.聽 Now if you open the app the text will have some space and margin in pdf document

Full Source Code

Wrapping it up this is the full source code of index.html file with javascript

index.html

123456789101112131415161718192021222324252627282930聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽jspdf encode pdf to base64 code聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽First Content聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽second content聽聽聽聽let element = document.getElementById('element')html2pdf(element,{聽聽聽聽margin:10,聽聽聽聽filename:'output.pdf',聽聽聽聽image:{type:'jpeg',quality:0.98},聽聽聽聽html2canvas:{scale:2,logging:true,dpi:192,letterRendering:true},聽聽聽聽jsPDF:{unit:'mm',format:'a4',orientation:'portrait'}})



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3